package com.example.rbq.coord

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout

/**
 *  跟随手指移动的View
 */
class DownMoveView @JvmOverloads constructor(context: Context,
                                             attrs: AttributeSet? = null) : View(context, attrs) {

    private var lastX = 0f
    private var lastY = 0f

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        // 相对于屏幕的位置
        val eventX = event.rawX
        val eventY = event.rawY
        when(event.action){
            MotionEvent.ACTION_MOVE ->{
                val layoutParams = this.layoutParams as CoordinatorLayout.LayoutParams
                layoutParams.leftMargin = (layoutParams.leftMargin + eventX - lastX).toInt()
                layoutParams.topMargin = (layoutParams.topMargin + eventY - lastY).toInt()
                setLayoutParams(layoutParams)
            }
            else ->{  }
        }
        lastX = eventX
        lastY = eventY
        return true
    }
}